package health.lm.com.shop.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.activity.XListActivity;
import health.lm.com.router.ARouterConstant;
import health.lm.com.shop.R;
import health.lm.com.shop.R2;
import health.lm.com.shop.adapter.HLCompanyLeaderAdapter;
import health.lm.com.shop.entity.HLCompanyLeaderSectionEntity;
import health.lm.com.shop.presenter.HLCompanyLeaderPresenter;

/**
 * Created by Android Studio .
 * Project：hlian health.lm.com.shop.activity
 * Date：2019/8/13 16:31
 * Introduce：商会领导
 */
@Route(path = ARouterConstant.HLCompanyLeaderActivity)
public class HLCompanyLeaderActivity extends XListActivity<HLCompanyLeaderPresenter> {
    @BindView(R2.id.title_bar)
    CommonTitleBar mTitleBar;
    @BindView(R2.id.rv_news)
    RecyclerView mRvNews;
    @BindView(R2.id.smart_refresh)
    SmartRefreshLayout mSmartRefresh;
    @Autowired
    public String id;
    private HLCompanyLeaderAdapter mLeaderAdapter;

    /**
     * 加载数据
     *
     * @param isRefresh true 刷新 false 加载更多
     * @param layout    头部布局
     * @param page      页数
     * @param pageSize  每页数量
     */
    @Override
    public void loadListData(boolean isRefresh, Object layout, int page, int pageSize) {
        getP().getData(id);
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_bar_refresh_list;
    }

    @Override
    public HLCompanyLeaderPresenter newP() {
        return new HLCompanyLeaderPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mTitleBar.getCenterTextView().setText("商会领导");
        mTitleBar.setListener((v, action, extra) -> finish());
        mLeaderAdapter = new HLCompanyLeaderAdapter(new ArrayList<>());
        super.recyclerView = mRvNews;
        super.rlRefreshLayout = mSmartRefresh;
        super.adapter = mLeaderAdapter;
        super.initData(savedInstanceState);
        mRvNews.setLayoutManager(new LinearLayoutManager(context));
        mRvNews.setAdapter(mLeaderAdapter);
        mLeaderAdapter.setOnItemClickListener((adapter1, view, position) -> {
            HLCompanyLeaderSectionEntity sectionEntity = (HLCompanyLeaderSectionEntity) adapter1.getData().get(position);
            if (sectionEntity.isHeader) return;
            ARouter.getInstance().build(ARouterConstant.HLPeopleDetailActivity)
                    .withString("title", sectionEntity.t.getName())
                    .withString("id", sectionEntity.t.getId())
                    .navigation();
        });

        getP().getData(id);
    }

    public void setNewData(List<HLCompanyLeaderSectionEntity> pNewData) {
        if (isRefresh) {
            mSmartRefresh.finishRefresh();
            mLeaderAdapter.setNewData(pNewData);
        }
        if (mLeaderAdapter.getData().size() <= 0) mLeaderAdapter.setEmptyView(empty());
    }
}
